package com.suixun.controller.onlinepay;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.suixun.dao.*;
import com.suixun.entity.*;
import com.suixun.service.Constants;
import com.suixun.service.DeviceService;
import com.suixun.service.ObjectService;
import com.suixun.service.OnlinepayService;
import com.suixun.util.enums.Payment;

@Controller
@RequestMapping(value = "/yiji")
public class YijiController {
    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    @Resource
    private DeviceService deviceService;
    @Resource
    private ObjectService objectService;
    @Resource
    private LogonlinepayMapper logonlinepayMapper;
    @Resource
    private LogincomeMapper logincomeMapper;
    @Resource
    private LogyijinotifyMapper logyijinotifyMapper;
    @Resource
    private OnlinepayService onlinepayService;

    @RequestMapping(value = "/notify")
    public void notify(PrintWriter out) throws Exception {
        try {
            HttpServletRequest request =
                    ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                            .getRequest();
            request.setCharacterEncoding("UTF-8");
            Map<String, String[]> paramMap = request.getParameterMap();
            ObjectMapper mapper = new ObjectMapper();
            mapper.getFactory().configure(JsonGenerator.Feature.ESCAPE_NON_ASCII, true);
            String jsonStr = mapper.writeValueAsString(paramMap);
            Logyijinotify logyijinotify = new Logyijinotify();
            logyijinotify.setAddtime(new Date());
            logyijinotify.setContent(jsonStr);
            logyijinotify.setType("notify");
            logyijinotifyMapper.insert(logyijinotify);
            
            if (request.getParameter("service") != null
                    && request.getParameter("service").equals(Constants.OP_REFUND_SERVICE)) {
                String orderNo = request.getParameter("orderNo");
                String resultCode = request.getParameter("resultCode");
                String resultMessage = request.getParameter("resultMessage");
                LogonlinepayExample logonlinepayExample = new LogonlinepayExample();
                logonlinepayExample.createCriteria().andOrdernoEqualTo(orderNo);
                List<Logonlinepay> logonlinepays =
                        logonlinepayMapper.selectByExample(logonlinepayExample);
                if (logonlinepays != null && logonlinepays.size() > 0) {
                    if(logonlinepays.get(0).getPaystatus() != null && logonlinepays.get(0).getPaystatus().contains("SUCCESS")){
                        out.write("failed");
                        return;
                    }
                    if (logonlinepays.get(0).getService().equals("refund")){
                        int deviceid = logonlinepays.get(0).getDeviceid();
                        int userid = logonlinepays.get(0).getUserid();
                        boolean isSuccess = false;
                        if ("EXECUTE_SUCCESS".equals(resultCode)) {
                        	isSuccess = true;
                        }
                        onlinepayService.refunded(userid, deviceid, isSuccess, orderNo, resultMessage, Payment.YIJI);
                        out.write("success");
                    }
                }else {
                    out.write("failed");
                }
            }
            out.flush();
            out.close();
        } catch (Exception e) {
            logger.error(e.toString(), e);
            out.write("failed");
            out.flush();
            out.close();
        }
    }

    @RequestMapping(value = "/test")
    public void test(PrintWriter out) throws Exception {
        out.write("testing");
        out.flush();
        out.close();
    }
}
